package us.codecraft.webmagic.lyon.liuhecai;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/6/10.
 */

public class DZHProcesso implements PageProcessor {


    public void process(Page page)  {
        Selectable url = page.getUrl();
        Html html = page.getHtml();
        Selectable selectable = html.xpath("div[@class='bd']");
        Selectable selectable1 = selectable.xpath("//tr[@class='infolist']");
        Selectable liuPingma = selectable1.xpath("//div[@class='sortDown clearfix']");
        Selectable tema = selectable1.xpath("//td[@class='bg-high2']//div[@class='p']");


        Selectable regex = liuPingma.regex("(\\d+){1}");
        Selectable regex1 = tema.regex("(\\d+){1}");

        List<String> tema_all = regex1.all();
        List<String> pingma_all = regex.all();
        Collections.reverse(tema_all);
        Collections.reverse(pingma_all);
        String content="";

        for (int i=0;i<tema_all.size();i++){
            String single="";
            for (int j=5;j>-1;j--){
                single=single+pingma_all.get(i*6+j)+" ";
            }
            single=single+" "+tema_all.get(i);

            content+=single+"\r\n";
        }

        Selectable regex2 = url.regex("(\\d+)");

        FileOutputStream fileOutputStream= null;
        try {
            fileOutputStream = new FileOutputStream(regex2.get()+".txt");
            byte[] bytes = content.getBytes();
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(content);
    }

    @Override
    public Site getSite() {
        return Site.me();
    }

    public static void main(String[] args) {
        DZHProcesso dzhProcesso = new DZHProcesso();

        List<String> list=new ArrayList<String>();
        for (int index=2000;index<2017;index++){
            list.add("https://www.dzh.vip/kj/kjjl/" + index + ".html");
        }



        String[] array =new String[list.size()];
        String[] strings1 = list.toArray(array);
        Spider.create(dzhProcesso).test(strings1);
    }
}
